const app = getApp();
const http = require('../../utils/util.js');
const mta = require('../../mta_sdk/mta_analysis.js');
var ins=null;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    indicatorDots: true,
    autoplay: true,
    interval: 5000,
    duration: 300,
    banner: [],
    imgList: [
      {
        types: 'iphone',
        name: "新iphone订单生成"
      },
      {
        types: 'zkz',
        name: "准考证"
      },
      {
        types:'jz',
        name:"最佳好孩子奖状"
      },
      
      {
        types: 'zs',
        name: "好儿子证书"
      },
      
      {
        types: 'hlj',
        name: "少先队员红领巾头像"
      },
      
      {
        types: 'card',
        name: "学生牌装饰"
      },
      {
        types: 'tz',
        name: "家长通知书"
      },
      {
        types: 'huaduo',
        name: "祖国花朵"
      }
    ],
    randTxt: '',
    isStart: false,
    isshow:1
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (opt) {
    mta.Page.init();
    this.getBanner();
    if (opt.appid && opt.page) {
      wx.navigateToMiniProgram({
        appId: opt.appid,
        path: opt.page,
        extraData: {},
        success(res) {
          console.log("openMini success")
        },
        fail: res => {
          console.log(res)
        }
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  startRand() {
    var randArr = [
      "/images/m_1.jpg", "/images/m_2.jpg", "/images/m_3.jpg", "/images/m_4.jpg", "/images/m_5.jpg",
    ];
    this.setData({
      isStart: true
    });

    ins = setInterval(res => {
      this.setData({
        randTxt: randArr[parseInt(randArr.length * Math.random())]
      });
    }, 100)
  },
  stopRand() {
    clearInterval(ins);
    this.setData({
      isStart: false
    })
  },
  login(e){
    var types = e.currentTarget.dataset.pid;
    console.log(e)
    var info = e.detail.userInfo
    //获取用户数据
    if (!info) {
      wx.showModal({
        title: '提示',
        content: '获取用户信息失败！',
      });
      return false;
    }
    var ssid = wx.getStorageSync("session_id");

    if (ssid && ssid != 'undefined' && ssid != '') {
      toPage(types)
      return false;
    }
    wx.showLoading({
      title: '登录中...',
    })
    wx.login({
      success: a => {
        wx.request({
          url: app.globalData.hosts + "Login/wxlogin",
          method: 'post',
          header: { 'content-type': 'application/x-www-form-urlencoded' },
          data: {
            code: a.code,
            appid: app.globalData.appid,
            secret: app.globalData.secret,
            info: JSON.stringify(info)
          },
          success: res => {
            console.log("请求成功")
            var r = res.data.data

            wx.setStorageSync('userInfo', info);
            wx.setStorageSync('openid', r.openid);
            wx.setStorageSync('session_id', r.session_id);
            wx.setStorageSync('member_id', r.member_id);
            toPage(types)
          },
          complete:res=>{
            wx.hideLoading();
          }
        })

      },
      fail: b => { },

    });

    //获取openid
    function toPage(types){
      wx.navigateTo({
        url: `/pages/${types}/${types}_make/${types}_make?pid=${types}`,
      });
    }
  },
  formSubmit: function (e) {
    app.sendForm(e);
  },
  getBanner(){
    wx.request({
      url: app.globalData.hosts +"Index/getimg",
      method: 'post',
      header: { 'content-type': 'application/x-www-form-urlencoded' },
      data: {
        appid: app.globalData.appid,
      },
      success: res => {
        this.setData({
          banner:res.data.data,
          isshow: res.data.msg
        });
        if (res.data.msg==0){
          wx.setNavigationBarTitle({
            title: '',
          })
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})